<template>
  <div>
    <img class="bg" src="@/assets/img/bg.jpg" />
    <el-form :model="ruleForm" :rules="rules" ref="addFormRef" class="login-form">
      <h1 class="h1">校园招聘系统注册</h1>
      <el-form-item v-if="tableName == 'yonghu'" label="用户名" prop="username">
        <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item v-if="tableName == 'yonghu'" label="姓名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item v-if="tableName == 'yonghu'" label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item v-if="tableName == 'yonghu'" label="电话" prop="mobile">
        <el-input v-model="ruleForm.mobile" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item v-if="tableName == 'yonghu'" label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <!-- 企业 -->
      <el-form-item
        v-if="tableName == 'company'"
        label="企业编号"
        prop="companyid"
      >
        <el-input
          v-model="ruleForm.companyid"
          placeholder="企业编号"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="tableName == 'company'"
        label="密码"
        prop="password"
      >
        <el-input type="password" v-model="ruleForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item
        v-if="tableName == 'company'"
        label="企业名称"
        prop="companyname"
      >
        <el-input
          v-model="ruleForm.companyname"
          placeholder="企业名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="tableName == 'company'"
        label="负责人"
        prop="principal"
      >
        <el-input v-model="ruleForm.principal" placeholder="负责人"></el-input>
      </el-form-item>
      <el-form-item
        v-if="tableName == 'company'"
        label="联系电话"
        prop="phone"
      >
        <el-input
          v-model="ruleForm.phone"
          placeholder="联系电话"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="tableName == 'company'"
        label="企业邮箱"
        prop="email"
      >
        <el-input
          v-model="ruleForm.email"
          placeholder="企业邮箱"
        ></el-input>
      </el-form-item>
      <el-button @click="login()" class="btn-login" type="primary"
        >注册</el-button
      >
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    //验证邮箱的规则
        var checkEmail=(rule,value,cb)=>{
            // 验证邮箱的正则表达式
            const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
            if (regEmail.test(value)) {
                // 合法的邮箱
                return cb()
            }
            cb(new Error('请输入合法的邮箱'))
        }
        // 验证手机号的规则
        var checkMobile = (rule, value, cb) => {
            // 验证手机号的正则表达式
            const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

            if (regMobile.test(value)) {
                return cb()
            }

            cb(new Error('请输入合法的手机号'))
        }
    return {
      ruleForm: {
        username:'',
        name:'',
        sex:'1',
        password:'',
        email:'',
        mobile:'',
        companyid:'',
        companyname:'',
        principal:'',
        email:'',
        phone:'',
      },
      tableName: '',
      rules: {
        username:[
            {required:true,message:'请输入用户名',trigger:'blur'},
            {min:3,max:10,message:'用户名的长度在3~10之间',trigger:'blur'}
        ],
        name:[
            {required:true,message:'请输入用户名',trigger:'blur'},
            {min:3,max:10,message:'用户名的长度在3~10之间',trigger:'blur'}
        ],
        password:[
            {required:true,message:'请输入密码',trigger:'blur'},
            {min:6,max:15,message:'密码的长度在6~15之间',trigger:'blur'}
        ],
        email:[
            {required:true,message:'请输入邮箱',trigger:'blur'},
            {validator:checkEmail,trigger:'blur'}
        ],
        mobile:[
            {required:true,message:'请输入手机',trigger:'blur'},
            {validator:checkMobile,trigger:'blur'}
        ],
        //企业相关
        companyid:[
            {required:true,message:'请输入用户名',trigger:'blur'},
            {min:3,max:10,message:'企业账号的长度在3~10之间',trigger:'blur'}
        ],
        companyname:[
            {required:true,message:'请输入用户名',trigger:'blur'},
            {min:1,max:10,message:'用户名的长度在1~10之间',trigger:'blur'}
        ],
        principal:[
            {required:true,message:'请输入负责人',trigger:'blur'},
            {min:2,max:15,message:'负责人的长度在6~15之间',trigger:'blur'}
        ],
        phone:[
            {required:true,message:'请输入手机',trigger:'blur'},
            {validator:checkMobile,trigger:'blur'}
        ],
      },
    }
  },
  mounted() {
    this.tableName=window.sessionStorage.getItem('tableName')
  },
  methods: {
    login(){
      if(this.tableName=='yonghu'){
        this.$refs["addFormRef"].validate(async valid=>{
            if (!valid) {
                return
            }
            const {data:res} =await this.$http.post('regyonghu',this.ruleForm)
            if(res.meta.status==201){
                this.$message.error(res.meta.msg)
                this.$router.push('/Login')
            }
            this.$message.success(res.meta.msg)
            
        })
            
      }
      if(this.tableName=='company'){
        this.$refs["addFormRef"].validate(async valid=>{
            if (!valid) {
                return
            }
            const {data:res} =await this.$http.post('regcompany',this.ruleForm)
            if(res.meta.status==201){
                this.$message.error(res.meta.msg)
                this.$router.push('/Login')
            }
            this.$message.success(res.meta.msg)
            
        })
      }

    }

    
    
  },
}
</script>
<style lang="scss" scoped>
</style>
